package com.ruoyi.luban.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.luban.domain.PUserAddress;
import com.ruoyi.luban.domain.WXUserLoginInfo;
import com.ruoyi.luban.domain.vo.LoginUserAndTokenVo;
import com.ruoyi.luban.service.IPUserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 微信用户地址Controller
 * 
 * @author ruoyi
 * @date 2024-01-28
 */
@RestController
@RequestMapping("/userAddress")
public class PUserAddressController extends WXBaseController
{
    @Autowired
    private IPUserAddressService pUserAddressService;

    /**
     * 新增微信用户地址
     */
    @Log(title = "微信用户地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PUserAddress pUserAddress,@RequestHeader(value = "token",defaultValue = "test")  String token)
    {
        WXUserLoginInfo info = this.getLoginUserInfo(token);
        if (info == null){
            return error("登录失效！");
        }
        pUserAddress.setOpenId(info.getOpenId());
        return toAjax(pUserAddressService.insertPUserAddress(pUserAddress));
    }

    /**
     * 修改微信用户地址
     */
    @Log(title = "微信用户地址", businessType = BusinessType.INSERT)
    @PutMapping
    public AjaxResult edit(@RequestBody PUserAddress pUserAddress,@RequestHeader(value = "token",defaultValue = "test")  String token)
    {
        WXUserLoginInfo info = this.getLoginUserInfo(token);
        if (info == null){
            return error("登录失效！");
        }
        return toAjax(pUserAddressService.updatePUserAddress(pUserAddress));
    }

    /**
     * 删除微信用户地址
     */
    @Log(title = "删除微信用户地址", businessType = BusinessType.INSERT)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids,@RequestHeader(value = "token",defaultValue = "test")  String token)
    {
        WXUserLoginInfo info = this.getLoginUserInfo(token);
        if (info == null){
            return error("登录失效！");
        }
        return toAjax(pUserAddressService.deletePUserAddressByIds(ids));
    }


    /**
     * 新增微信用户地址
     */
    @Log(title = "添加微信用户地址", businessType = BusinessType.INSERT)
    @PostMapping("addAddress")
    public AjaxResult addAddress(@RequestBody PUserAddress pUserAddress,@RequestHeader(value = "token",defaultValue = "test")  String token)
    {
        //登录用户
        LoginUserAndTokenVo info = this.getLoginUserInfo2(token);
        if (info == null){
            return error_to_login();
//            return error("当前登录用户不存在");
        }
        if(pUserAddress.getId() == null) {//添加学校
            if (info.getSchoolId() != null && !info.getSchoolId().equals(pUserAddress.getSchoolId())) {
                return error("当前登录用户已绑定学校，不可绑定其他学校");
            }
        }
        LoginUserAndTokenVo loginUserAndTokenVo = pUserAddressService.addUserAddress(token, info, pUserAddress);
        if(loginUserAndTokenVo !=null) {
            setLoginUserInfo2(loginUserAndTokenVo);
            return success(loginUserAndTokenVo);
        }
        return error("学校登录信息不存在");
    }

    /**
     * 查询微信用户地址列表
     */
    @GetMapping("/list")
    @Log(title = "查询微信用户地址列表", businessType = BusinessType.INSERT)
    public TableDataInfo list(PUserAddress pUserAddress,@RequestHeader(value = "token",defaultValue = "test")  String token)
    {
        LoginUserAndTokenVo info = this.getLoginUserInfo2(token);
        if (info == null){
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(HttpStatus.ERROR_LOGIN);
            rspData.setMsg("登录失效！");
            return rspData;
        }
        startPage();
        pUserAddress.setOpenId(info.getMaOpenId());
        List<PUserAddress> list = pUserAddressService.selectPUserAddressList(pUserAddress);
        return getDataTable(list);
    }
}
